package com.Crawlzongheng;

import java.util.concurrent.TimeUnit;

import com.Model.NovelChapterModel;
import com.Model.NovelReadMode;
import com.zonghengdb.ZongHengDB;


public class ReadPageThread extends Thread{
	private boolean flag =false;
	
	public ReadPageThread(String name) {
		super(name);
	}
	
	@Override
	public void run() {
		flag = true;
		ZongHengDB  db = new ZongHengDB();
		while(flag){
			try {
				NovelChapterModel chapter = db.getRandChapter(1);
				if(null != chapter){
					ReadPage readPage = new ReadPage(chapter.getUrl());
					NovelReadMode novel = readPage.analyzer();
					if(null == novel){
						continue;
					}
					novel.setChapterId(chapter.getChapterId());
					novel.setChapterTime(chapter.getChapterTime());
					db.saveNovelRead(novel);
					db.updateChapterState(chapter.getId(), 0);
					TimeUnit.MILLISECONDS.sleep(500);
				}else {
					TimeUnit.MILLISECONDS.sleep(1000);

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		super.run();
	}
	
	
	
	public static void main(String[] args) {
		ReadPageThread thread = new ReadPageThread("ReadPageThread");
		thread.start();
	}
}
